import axios from "axios";
import { HWMI_URLS } from "../../modules/Api";

import { ref,watch } from "vue";

export default function ({ emit }, { successEvent, errorEvent }) {
    const relationFrom = ref("");
    const relationFromType = ref("");
    const relationTo = ref("");
    const relationToType = ref("");
    const relationLabel = ref("");

    // 关系操作按钮的加载中图标是否显示
    const isRelationLoadingShow = ref(false);

    // 自动拆分功能
    watch(relationFrom, (n, o) => {
        const splitted = n.split("..");
        if (splitted.length >= 2 && splitted[0] !== "" && splitted[1] != "") {
            relationFrom.value = splitted[0];
            relationFromType.value = splitted.slice(1).join("");
        }
    });

    watch(relationTo, (n, o) => {
        const splitted = n.split("..");
        if (splitted.length >= 2 && splitted[0] !== "" && splitted[1] != "") {
            relationTo.value = splitted[0];
            relationToType.value = splitted.slice(1).join("");
        }
    });

    // 添加关系
    const addRelation = () => {
        if (relationFrom.value === ""
            || relationFromType.value === ""
            || relationTo.value === ""
            ||relationToType.value===""
            || relationLabel.value === "") {
            emit(errorEvent, "请输入所有字段");
            return;
        }

        isRelationLoadingShow.value = true;

        axios.post(HWMI_URLS.managementUrls.graphDbManagementUrls.addRelationUrl, {
            from: relationFrom.value,
            fromType:relationFromType.value,
            to: relationTo.value,
                toType:relationToType.value,
                label: relationLabel.value
            })
            .then(res => {
                if (res.data.success) {
                    emit(successEvent, "成功新增关系");
                }
                else {
                    emit(errorEvent, res.data.message);
                }
            })
            .catch(reason => {
                console.log(reason);
                emit(errorEvent, reason);
            })
            .finally(() => {
                isRelationLoadingShow.value = false;
            });
    };

    // 修改关系
    const modifyRelation = () => {
        if (relationFrom.value === ""
            || relationFromType.value === ""
            || relationTo.value === ""
            || relationToType.value === ""
            || relationLabel.value === "") {
            emit(errorEvent, "请输入所有字段");
            return;
        }

        isRelationLoadingShow.value = true;

        axios.post(HWMI_URLS.managementUrls.graphDbManagementUrls.modifyRelationUrl, {
            from: relationFrom.value,
            fromType: relationFromType.value,
            to: relationTo.value,
            toType: relationToType.value,
            label: relationLabel.value
        })
            .then(res => {
                if (res.data.success) {
                    emit(successEvent, "成功修改关系");
                }
                else {
                    emit(errorEvent, res.data.message);
                }
            })
            .catch(reason => {
                console.log(reason);
                emit(errorEvent, reason);
            })
            .finally(() => {
                isRelationLoadingShow.value = false;
            });
    };

    // 删除关系
    const removeRelation = () => {
        if (relationFrom.value === ""
            || relationFromType.value === ""
            || relationTo.value === ""
            || relationToType.value === ""
            || relationLabel.value === "") {
            emit(errorEvent, "请输入所有字段");
            return;
        }

        isRelationLoadingShow.value = true;

        axios.post(HWMI_URLS.managementUrls.graphDbManagementUrls.removeRelationUrl, {
            from: relationFrom.value,
            fromType: relationFromType.value,
            to: relationTo.value,
            toType: relationToType.value,
            label: relationLabel.value
        })
            .then(res => {
                if (res.data.success) {
                    emit(successEvent, "成功删除关系");
                }
                else {
                    emit(errorEvent, res.data.message);
                }
            })
            .catch(reason => {
                console.log(reason);
                emit(errorEvent, reason);
            })
            .finally(() => {
                isRelationLoadingShow.value = false;
            });
    };

    return {
        relationFrom,
        relationFromType,
        relationTo,
        relationToType,
        relationLabel,

        isRelationLoadingShow,

        addRelation,
        modifyRelation,
        removeRelation
    };
}